<?
/**
 * VoiceOne Settings/Extensions/Cid
 *
 * Frontend to provide access to the extensions system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 */

  include ("../../../admin/config/cfg.php");
  global $cwlang;

  //================ POST & GET =================
  $alert=getAlert();
  //delete cid
  if((isset($_GET["del"]))&&($_GET["del"]!="")){
  	//delete extension's settings
    $ret=$asterisk->delCid(0,$_GET["del"]);
    if($ret>0){
    	$alert="<div class=\"AlertGood\">".T_("Extension's cid settings has been deleted")."</div>";
      //if extension is a virtual extension erase also the linked clients settings
      $virtualArr=$asterisk->getVirtualExtension($_GET["del"]);
      if((is_array($virtualArr))&&(is_array($virtualArr[0]))&&(is_array($virtualArr[0]["linked_clients"]))){
      	foreach($virtualArr[0]["linked_clients"] as $client){
        	$ret=$asterisk->delCid(0,$client);
          if($ret<1){
          	$alert="<div class=\"AlertBad\">".T_("Extension's cid settings hasn't been deleted")."</div>";
            break;
          }
        }
      }
    }else $alert="<div class=\"AlertBad\">".T_("Extension's cid settings hasn't been deleted")."</div>";
    goToMainPage($alert,"./");
  }

  //================ PAGE LOGIC ================
  //get trunks
  $trunkArr=$asterisk->getTrunk(0,0);
  if(is_array($trunkArr)){
    $ret=array();
    foreach($trunkArr as $trunk){
      //get cid settings
      $settingArr=array();
      $cidArr=$asterisk->getCid($trunk["id"],0);
      if(is_array($cidArr)){
        foreach($cidArr as $cid){
          $c=array(
              "clir"        => $cid["clir"],
              "cid_number"  => $cid["cid_number"],
              "cid_name"    => $cid["cid_name"]
            );
          $settingArr[$cid["extension"]]=$c;
        }
      }
      $ret[$trunk["name"]]=$settingArr;
    }
  }
  //get single extensions
  $clientArr=array();
  $blacklist=array();
  $virtualArr=$asterisk->getVirtualExtension(0);
  if(is_array($virtualArr)){
  	foreach($virtualArr as $virtual){ 
    	//add virtual extensions to client list
  		$clientArr[]=array("id"=>$virtual["id"], "extension"=>$virtual["extension"]);
      $blacklist=array_merge($blacklist,$virtual["linked_clients"]);
		}
 	}
 	//get single extensions
  $extArr=$asterisk->getClient(0,0);
  if(is_array($extArr)){
  	foreach($extArr as $ext){
    	//skip extensions that are contained in a virtual extension
    	if((is_array($blacklist))&&(in_array($ext["id"],$blacklist,true))){
      	continue;
      }
      //add single extensions to client list
  		$clientArr[]=array("id"=>$ext["id"], "extension"=>$ext["extension"]);
		}
 	}
  // build html
  $html="";
  if(is_array($ret)){
    if(is_array($clientArr)){
      $trunkcount=0;
      //create trunk row
      $html.="<tr>";
      $html.="  <th class=\"small none \">&nbsp;</th>";
      $first="first";
      foreach($ret as $trunk => $v){
        list($trunk_tech,$trunk_name)=split("/",$trunk);
        $html.="  <th class=\"$first bordered\">".$trunk_name."<br/><span style=\"font-size: 10px;\">[".$trunk_tech."]</span></th>";
        $first="";
        $trunkcount++;
      }
      $html.="  <th class=\"small none\">&nbsp;</th>";
      $html.="</tr>";
      //create clients rows
      foreach($clientArr as $client){
        $html.="<tr>";
        $html.="  <td class=\"first small\"><span class=\"ext_list\"><a href=\"./edit/?id=".$client["id"]."\" style=\"cursor: pointer;\">".$client["extension"]."</a></span></td>";
        foreach($ret as $trunk){
          if((is_array($trunk))&&(in_array($client["id"],array_keys($trunk)))){
            if(($trunk[$client["id"]]["clir"]==1)||($trunk[$client["id"]]["clir"]=="1")) $clir="ON"; else $clir="OFF";
            $title="''".$trunk[$client["id"]]["cid_name"]."'' <".$trunk[$client["id"]]["cid_number"]."> [Clir: ".$clir."]";
            $html.="<td><img src=\"$relBasePath/public/img/ok.gif\" title=\"$title\" /></td>";
          }else{
            $html.="  <td>&nbsp;-&nbsp;</td>";
          }
        }
        $html.="  <td><img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/trash.gif\" onclick=\"if(confirm('".T_("Are you sure you want to remove cid settings?")."')) location.href='./?del=".$client["id"]."'; else return false;\" /></td>";
        $html.="</tr>";
      }
    }else{
        $html.="<tr>";
        $html.="  <td class=\"first top\" colspan=\"".(intval($trunkcount)+2)."\"><span style=\"font-size: 10px; font-weight: bold;\">".T_("There are no defined extensions")."</span></td>";
        $html.="</tr>";
    }
  }else{
    $html.="<tr>";
    $html.="  <td class=\"first top\"><span style=\"font-size: 10px; font-weight: bold;\">".T_("There are no defined trunks")."</span></td>";
    $html.="</tr>";
  }
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
    <div class="SectionHeader">
      <?=$alert?>
      <h1><?=T_("Caller ID")?></h1>
    </div>
    <table id="ext_table" cellpadding="0" cellspacing="0" width="100%">
      <?=$html?>
    </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start();
  ?>
    <div class="Sidebar">
      <?$sel="cid";include("../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
    </div>
  <?
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
  $smarty->assign("section", "extensions");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

